
package com.xpgk.mvc.statistic.employeePerformanceList;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bouncycastle.crypto.tls.MACAlgorithm;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bloseWeight.DelWeiTarget;
import com.xpgk.mvc.statistic.dailyNursing.DailyNursingStatisticSQL;
import com.xpgk.mvc.statistic.loseWeightList.LoseWeightListSQL;
import com.xpgk.mvc.statistic.spropagandaTraining.SpropagandaTrainingSQL;
import com.xpgk.tools.ToolDateTime;


@Service(name = EmployeePerformanceListService.serviceName)
public class EmployeePerformanceListService extends BaseService{

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(EmployeePerformanceListService.class);
	public static final String serviceName = "employeePerformanceListService";
	
	
	/*
	 * 店员资料查询
	 */
	
	public List<Record> employeeQuery(String organIds,String startdate,String enddate){
		//店员基本信息
		List<Record> result = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_employeequery),organIds,startdate,enddate);
		//减重情况
		for(Record r : result){
			List<Record> queryloseweightsituation = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_queryloseweightsituation),r.get("ids").toString(),startdate,enddate);
			float loseweightplan = 0;
			float loseweightnumber = 0;
			for(Record f : queryloseweightsituation){
				loseweightplan = loseweightplan+Float.parseFloat(f.get("delWeight").toString());
				loseweightnumber = loseweightnumber+Float.parseFloat(f.get("loseweightnumber").toString());
			}
			float goalachievementrate = loseweightnumber/loseweightplan*100;
			r.set("loseweightplan",(float)(Math.round(loseweightplan*100))/100);
			r.set("loseweightnumber",(float)(Math.round(loseweightnumber*100))/100);
			r.set("goalachievementrate",(float)(Math.round(goalachievementrate*100))/100);
			//服务次数
			List<Record> servicetime = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_servicetime),startdate,enddate,r.get("ids").toString(),r.get("ids").toString());
			for(Record rr : servicetime){
				r.set("servicetime",rr.get("servicetime").toString());
			}
			//塑型次数
			List<Record> moldinglist = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_molding),r.get("ids").toString(),startdate,enddate);
			for(Record moiding : moldinglist){
				r.set(moiding.get("code").toString(),moiding.get("times").toString());
			}
			//销售金额
			List<Record> sellsumlist = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_sellsum),r.get("ids").toString(),startdate,enddate);
			double loseweightsum = 0;
			double serviceproductsum = 0;
			double otherproductsum = 0;
			for(Record sellsum : sellsumlist){
				if(sellsum.get("useids").toString().equals("283319421dd040ecbd983d2aab94d4f2")){
					loseweightsum = loseweightsum+Double.parseDouble(sellsum.get("sellsum").toString());
				}else if(sellsum.get("useids").toString().equals("f251248c324f436c8b8ff21a4a06bbc1")){
					serviceproductsum = serviceproductsum + Double.parseDouble(sellsum.get("sellsum").toString());
				}else{
					otherproductsum = otherproductsum + Double.parseDouble(sellsum.get("sellsum").toString());
				}
			}
			r.set("loseweightsum", loseweightsum);
			r.set("serviceproductsum", serviceproductsum);
			r.set("otherproductsum", otherproductsum);
			//参加培训次数
			List<Record> visittrainingtimeslist = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_visittrainingtimes),startdate,enddate);
			int trainingtimes=0;
			for(Record visit : visittrainingtimeslist){
				String[] splitarr = visit.get("trainingtimes").toString().split(",");
				for(int i =0;i<splitarr.length;i++){
					if(r.get("employeename").equals(splitarr[i].toString())){
						trainingtimes++;
					}
				}
			}
			r.set("trainingtimes",trainingtimes);
			//回访次数
			List<Record> visitcustomernumberlist = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_visitcustomernumber),r.get("ids").toString(),startdate,enddate);
			for(Record v : visitcustomernumberlist){
				r.set("visitcustomernumber", v.get("visitcustomernumber").toString());
				
			}
			//转介绍人数
			List<Record> numberofintroductionslilst = Db.find(getSqlMy(EmployeePerformanceListSQL.semployeeperformancelist_numberofintroductions),r.get("ids").toString(),startdate,enddate);
			for(Record number : numberofintroductionslilst){
				r.set("numberofintroductions", number.get("numberofintroductions").toString());
			}
			//宣传册数量
		}
		return result;
	}
}
